﻿using Gx.Bll.Manage;
using Gx.ColdWarehouseErp.Controllers;
using Gx.Common;
using Gx.Model.Entity;
using Gx.Model.Enums;
using Gx.Model.ViewModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace Gx.ZhiChengXianWei.Controllers
{
    public class LKZeroWarehouseContractController : BaseController
    {
        protected LKZeroWarehouseContractBll lKZeroWarehouseContractBll = new LKZeroWarehouseContractBll();
        protected LKBillTemplateBll templateBll = new LKBillTemplateBll();
        //
        // GET: /LKZeroWarehouseContract/

        public ActionResult List()
        {
            return View();
        }
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="customerInput"></param>
        /// <returns></returns>
        public JsonResult GetLKZeroWarehouseContractPageList(LKZeroWarehouseContractInput input)
        {
            var result = lKZeroWarehouseContractBll.GetLKZeroWarehouseContractPageList(input);
            return JsonList(result.Total, result.List.ToArray());
        }

        /// <summary>
        /// 获取零仓合同
        /// </summary>
        /// <returns></returns>
        public JsonResult GetLKZeroWarehouseContract()
        {
            var result = lKZeroWarehouseContractBll.GetLKZeroWarehouseContracts();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 新增零仓合同
        /// </summary>
        /// <param name="contract"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddLKZeroWarehouseContract(LKZeroWarehouseContractDto contract)
        {
            var result = lKZeroWarehouseContractBll.AddLKZeroWarehouseContract(contract);
            //string message = result ? "新增成功" : "新增失败";
            return Json(result.Success, result.Msg);
        }

        /// <summary>
        /// 编辑零仓合同
        /// </summary>
        /// <param name="contract"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditLKZeroWarehouseContract(LKZeroWarehouseContractDto contract)
        {
            var result = lKZeroWarehouseContractBll.EditLKZeroWarehouseContract(contract);
            string message = result ? "编辑成功" : "编辑失败";
            return Json(result, message);
        }

        /// <summary>
        /// 删除零仓合同
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteLKZeroWarehouseContract(int id)
        {
            var result = lKZeroWarehouseContractBll.DeleteLKZeroWarehouseContract(id);
            return Json(result.Success, result.Msg);
            //string message = result ? "删除成功" : "删除失败";
            //return Json(result, message);
        }

        /// <summary>
        /// 添加零仓合同视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AddLKZeroWarehouseContract()
        {
            var temp = templateBll.GetLKBillTemplateByBillType((int)BillType.ZeroWarehouseContract);
            var contract = JsonConvert.DeserializeObject<LKZeroWarehouseContract>(temp?.TemplateContent);
            if (contract != null) 
            {
                contract.CreateUser = LoginHelper.UserName();
                contract.CreateUserId = LoginHelper.GetUserId();
                contract.CreateTime = DateTime.Now;
                contract.ModifyTime = DateTime.Now;
                contract.ModifyUserId = LoginHelper.GetUserId();
                contract.ModifyUser = LoginHelper.UserName();
            }
            return View(contract);
        }

        /// <summary>
        /// 添加零仓合同分部视图
        /// </summary>
        /// <param name="domId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult _AddLKZeroWarehouseContract(int domId)
        {
            return PartialView(domId);
        }

        /// <summary>
        /// 编辑零仓合同
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult EditLKZeroWarehouseContract(int id)
        {
            var result = lKZeroWarehouseContractBll.GetLKZeroWarehouseContractById(id);
            return View(result);
        }
    }
}

